<?php
// +----------------------------------------------------------------------
// | Citrix [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.tpCitrix.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.tpCitrix.com/ )
// +----------------------------------------------------------------------
// | Author: Citrix < 2722279500@qq.com >
// +----------------------------------------------------------------------

namespace app\index\controller;

use \think\Db;
use \think\Cache;
use \think\Cookie;
use \think\Session;
use \think\Controller;
class Common extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->assign('cur',"common");
    }

	//登录
    public function login()
    {
        if($this->request->isAjax()) 
        {
        	//是提交操作
			$post = $this->request->post();
			$validate = new \think\Validate([
                ['user', 'require', '账号不能为空'],
                ['pass', 'require', '密码不能为空'],
                ['_chkcode','require|captcha','验证码不能为空|验证码不正确'],

            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                return $this->error('提交失败：' . $validate->getError());
            }
            $post['pass'] = password($post['pass']);
            if($info = Db::name('users')->where(['user'=>$post['user'],'newpass'=>$post['pass']])->find())
            {
            	if(!empty($info['status']))
            	{
            		return $this->error('账号被禁，请联系系统管理员');
            	}
            	session::set('user_id',$info['id']);
            	return $this->success('登录成功','index/index/index');
            }else
            {
            	return $this->error('提交失败：账号不存在' );
            }
        }else
        {
        	return $this->fetch();
        }
    }
	//注册
    public function register()
    {
        if($this->request->isAjax()) 
        {
        	//是提交操作
			$post = $this->request->post();
			//验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['user', 'require', '账号不能为空'],
                ['newpass', 'require', '密码不能为空'],
                ['chkpass', 'require', '确认密码不能为空'],
                ['fullname', 'require', '姓名不能为空'],
                ['company', 'require', '公司全称不能为空'],
                ['m_title', 'require', '所在部门不能为空'],
                ['email', 'require', '邮箱不能为空'],
                ['mobile', 'require', '手机不能为空'],
                ['captcha','require|captcha','验证码不能为空|验证码不正确'],
                ['is_ok', 'require', '注册协议未同意'],

            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                return $this->error('提交失败：' . $validate->getError());
            }
            if(!preg_match("/^1[34578]\d{9}$/", $post['mobile']))
            {
                return $this->error('提交失败：手机格式不正确');
            }
            if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/",$post['email']))
            {
                return $this->error('提交失败：邮箱格式不正确');
            }
            if(password($post['newpass']) != password($post['chkpass']))
            {
                return $this->error('提交失败：二次输入密码不正确');
            }
            if(!preg_match("/^[a-z\d]*$/i",$post['user']))
            {
                return $this->error('账号只能包含数字和字母');
            }
            if(true == Db::name('users')->where('user',$post['user'])->count())
            {
                return $this->error('账号已存在');
            }
            $post['newpass'] = password($post['newpass']);
            unset($post['captcha']);
            unset($post['is_ok']);
            unset($post['chkpass']);
            $post['create_time'] = time();
            $post['ip'] = $this->request->ip();
            $post['status'] = Db::name('webconfig')->where('web','web')->value('user_status');
            if(true == Db::name('users')->insert($post))
            {
            	return $this->success('注册成功','index/common/login');
            }else
            {
            	return $this->error('提交失败：请留下系统管理员');
            }
        }else
        {
        	return $this->fetch();
        }
    }
    public function quit()
    {
        Session::delete('user_id');
        return $this->success('退出成功','index/index/index');
    }
    public function download()
    {
        $path = input('path/s',0);
        $id = input('id/d',0);
        if(empty($path) || empty($id))
        {
            echo json_encode(array('code'=>1,'msg'=>'数据不存在'));exit;
        }
        $web_config = Db::name('webconfig')->where('web','web')->find();
        //需要登录才可以下载
        if($web_config['download_status'] == 1)
        {   
            if(empty($this->user_id))
            {
                echo json_encode(array('code'=>1,'msg'=>'需要登录才可以下载'));exit;
            }
        }
        echo json_encode(array('code'=>0,'msg'=>'操作成功','data'=>geturl($id)));exit;
    } 
    public function search()
    {
        $keywords = input('keywords/s');

        $page = input('page/d',1);

        $count = Db::name('goods')->where(['status'=>1])->where('title','like','%'.$keywords.'%')->count();

        $limit = $page-1;
        $goods = Db::name('goods')->where(['status'=>1])->where('title','like','%'.$keywords.'%')->order('sort DESC')->limit($limit,20)->select();

        $pager = pager($count,$page,20,6,'/common/search/keywords/'.$keywords);

        $this->assign('pager',htmlspecialchars_decode($pager));
        $this->assign('goods',$goods);
        $this->assign('keywords',$keywords);
        $this->assign('web_title',$keywords.'-搜索结果');
        return $this->fetch();
    }

}
